package com.cochain.dynamicdatasource.controller;

import com.cochain.dynamicdatasource.DBChangeService;
import com.cochain.dynamicdatasource.DBContextHolder;
import com.cochain.dynamicdatasource.entity.po.Database;
import com.cochain.dynamicdatasource.entity.po.Tables;
import com.cochain.dynamicdatasource.service.DataBaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class DatabaseController {
    @Autowired
    private DataBaseService dataBaseService;

    @Autowired
    private DBChangeService dbChangeServiceImpl;

    @GetMapping("/testShowDataBases")
    public String testShowDataBases() throws Exception {
        //切换到数据库dbtest2
        String datasourceId="im-vue";
        dbChangeServiceImpl.changeDb(datasourceId);
        List<Database> userList= dataBaseService.get();
        System.out.println(userList.toString());

        //再切换到数据库dbtest3
        dbChangeServiceImpl.changeDb("ry-vue");
        List<Tables> userList3= dataBaseService.getTable();
        System.out.println(userList3.toString());

        //切回主数据源
        DBContextHolder.clearDataSource();
        return "ok";
    }
}
